/**
 * 
 * @author Huang Junjie
 * 
 */

package TimeSqueeze.logic;

import java.util.ArrayList;
import TimeSqueeze.common.*;
import TimeSqueeze.storage.TempStorage;

public class Display implements CommandInterface {

	private String commandContent;
	private ArrayList<Task> untimedTasks = new ArrayList<Task>();
	private ArrayList<DeadlineTask> deadlineTasks = new ArrayList<DeadlineTask>();
	private ArrayList<TimedTask> timedTasks = new ArrayList<TimedTask>();

	public enum CONTENT_TYPE {
		TIMED, UNTIMED, DEADLINE, ALL, ILLEGAL, DONE
	}

	private CONTENT_TYPE determineDisplayContentType() {
		switch (commandContent.toLowerCase()) {
		case "timed":
			return CONTENT_TYPE.TIMED;
		case "untimed":
			return CONTENT_TYPE.UNTIMED;
		case "deadline":
			return CONTENT_TYPE.DEADLINE;
		case "all":
			return CONTENT_TYPE.ALL;
		case "done":
			return CONTENT_TYPE.DONE;
		default:
			return null;
		}
	}

	public Display(String paras) {
		commandContent = paras;
	}

	@Override
	public void execute() {
		CONTENT_TYPE content_type = determineDisplayContentType();
		switch (content_type) {
		case TIMED:
			addUndoneTimedTasksToDisplay();
			break;
		case UNTIMED:
			addUndoneUntimedTasksToDisplay();
			break;
		case DEADLINE:
			addUndoneDeadlineTasksToDisplay();
			break;
		case ALL:
			addUndoneDeadlineTasksToDisplay();
			addUndoneTimedTasksToDisplay();
			addUndoneUntimedTasksToDisplay();
			break;
		case DONE:
			addDoneTasksToDisplay();
			break;
		case ILLEGAL:
			return;
		}
		displayResults();
	}

	private void displayResults() {
		
		if(untimedTasks.size()!=0){
			System.out.println("*****Untimed Tasks*****");
		}
		
		for (int i = 0; i < untimedTasks.size(); i++){		
			System.out.println(untimedTasks.get(i).getDescription());
			System.out.println("Tag: "+untimedTasks.get(i).getTag());
			System.out.println();
		}
		
		if(timedTasks.size()!=0){
			System.out.println("*****Timed Tasks*****");
		}

		for (int i = 0; i < timedTasks.size(); i++){
			System.out.println(timedTasks.get(i).getDescription());
			System.out.println("Start Time: " + timedTasks.get(i).getStartTime().toString()
					+"  " + "End Time: " + timedTasks.get(i).getEndTime().toString());
			System.out.println("Tag: " + timedTasks.get(i).getTag());
			System.out.println();
		}

		if(deadlineTasks.size()!=0){
			System.out.println("*****Deadline Tasks*****");
		}
		for (int i = 0; i < deadlineTasks.size(); i++){
			System.out.println(deadlineTasks.get(i).getDescription());
			System.out.println("Deadline: " + deadlineTasks.get(i).getEndTime().toString());
			System.out.println("Tag: " + deadlineTasks.get(i).getTag());
			System.out.println();
		}
				
	}

	private void addDoneTasksToDisplay() {
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if (TempStorage.deadlineTasks.get(i).getDone()) {
				deadlineTasks.add((DeadlineTask) TempStorage.deadlineTasks
						.get(i));
			}
		}

		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (TempStorage.timedTasks.get(i).getDone()) {
				timedTasks.add((TimedTask) TempStorage.timedTasks.get(i));
			}
		}

		for (int i = 0; i < TempStorage.untimedTasks.size(); i++) {
			if (TempStorage.untimedTasks.get(i).getDone()) {
				untimedTasks.add((Task) TempStorage.untimedTasks.get(i));
			}
		}
	}

	private void addUndoneUntimedTasksToDisplay() {
		for (int i = 0; i < TempStorage.untimedTasks.size(); i++) {
			if(!TempStorage.untimedTasks.get(i).getDone()){
			untimedTasks.add((Task) TempStorage.untimedTasks.get(i));
			}
		}
	}

	private void addUndoneTimedTasksToDisplay() {
		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (!TempStorage.timedTasks.get(i).getDone()) {
				timedTasks.add((TimedTask) TempStorage.timedTasks.get(i));
			}
		}

	}

	private void addUndoneDeadlineTasksToDisplay() {
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if(!TempStorage.deadlineTasks.get(i).getDone()){
				deadlineTasks.add((DeadlineTask) TempStorage.deadlineTasks.get(i));
			}
		}
	}

	@Override
	public void undo() {
	}
	
	
}